/*     */ package dm.jdbc.driver;
/*     */ 
/*     */ import dm.jdbc.dataConvertion.Convertion;
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import dm.jdbc.dbaccess.DmMsgSend;
/*     */ import dm.jdbc.desc.ParameterDesc;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmReaderBinder
/*     */   extends DmBinder
/*     */ {
/*  16 */   boolean toBindLobCyc = false;
/*     */   
/*  18 */   boolean new_lob_flag = false;
/*     */   
/*  20 */   byte[] ret = null;
/*     */   
/*     */   public DmReaderBinder(DmdbPreparedStatement_bs pstmt)
/*     */   {
/*  24 */     super(pstmt);
/*  25 */     this.new_lob_flag = pstmt.getConnection_bs().getNewLobFlag();
/*     */   }
/*     */   
/*     */   void init()
/*     */   {
/*  30 */     this.recDType = 19;
/*  31 */     this.recPrec = Integer.MAX_VALUE;
/*  32 */     this.recScale = 0;
/*     */   }
/*     */   
/*     */   void checkType(int icol, boolean forBatch) throws SQLException
/*     */   {
/*  37 */     if (this.m_checkType) {
/*  38 */       return;
/*     */     }
/*  40 */     this.m_checkType = true;
/*     */     
/*  42 */     if (this.paramsDesc == null) {
/*  43 */       this.paramsDesc = this.pstmt.getParamsDesc();
/*     */     }
/*  45 */     byte typeFlag = this.paramsDesc[icol].getTypeFlag();
/*  46 */     int dType = this.paramsDesc[icol].getDType();
/*  47 */     int prec = this.paramsDesc[icol].getPrec();
/*  48 */     int scale = this.paramsDesc[icol].getScale();
/*  49 */     byte ioType = this.paramsDesc[icol].getIOType();
/*     */     
/*  51 */     this.destIoType = ioType;
/*  52 */     this.srcScale = scale;
/*     */     
/*  54 */     if ((typeFlag == 1) || (ioType == 1))
/*     */     {
/*  56 */       this.destDType = dType;
/*  57 */       this.destPrec = prec;
/*  58 */       this.destScale = scale;
/*  59 */       return;
/*     */     }
/*     */     
/*  62 */     switch (dType)
/*     */     {
/*     */     case 0: 
/*     */     case 1: 
/*     */     case 2: 
/*     */     case 17: 
/*     */     case 18: 
/*  69 */       dType = 2;
/*  70 */       prec = 8188;
/*  71 */       scale = 0;
/*  72 */       break;
/*     */     
/*     */     case 12: 
/*     */     case 19: 
/*  76 */       dType = 19;
/*  77 */       prec = Integer.MAX_VALUE;
/*  78 */       scale = 0;
/*  79 */       break;
/*     */     
/*     */     default: 
/*  82 */       DBError.throwSQLException(6007);
/*     */     }
/*     */     
/*  85 */     this.destDType = dType;
/*  86 */     this.destPrec = prec;
/*  87 */     this.destScale = scale;
/*     */   }
/*     */   
/*     */   private String readLeftCompletelyWithTry(DmReaderParam readerParam, String strTry)
/*     */     throws IOException
/*     */   {
/*  93 */     Reader reader = readerParam.m_reader;
/*  94 */     long targetLen = readerParam.m_targetLen;
/*     */     
/*  96 */     if (this.streamReadOver)
/*     */     {
/*  98 */       return strTry;
/*     */     }
/*     */     
/* 101 */     StringBuffer strBuf = new StringBuffer(strTry);
/*     */     
/* 103 */     int maxLen = 8000;
/* 104 */     char[] totalBuf = new char[maxLen];
/*     */     
/* 106 */     int count = this.readerTryLen;
/*     */     for (;;)
/*     */     {
/* 109 */       int readLen = reader.read(totalBuf, 0, maxLen);
/* 110 */       if (readLen <= 0) {
/*     */         break;
/*     */       }
/* 113 */       if ((targetLen != -1L) && (count + readLen >= targetLen))
/*     */       {
/* 115 */         String tmp = new String(totalBuf, 0, (int)(targetLen - count));
/* 116 */         strBuf.append(tmp);
/*     */         
/* 118 */         break;
/*     */       }
/*     */       
/*     */ 
/* 122 */       count += readLen;
/*     */       
/* 124 */       String tmp = new String(totalBuf, 0, readLen);
/* 125 */       strBuf.append(tmp);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 130 */     this.streamReadOver = true;
/*     */     
/* 132 */     return strBuf.toString();
/*     */   }
/*     */   
/*     */   public int bindData(DmMsgSend msg, int irow, int icol, boolean forBatch) throws SQLException
/*     */   {
/* 137 */     DmReaderParam readerParam = ((DmReaderParam[])this.pstmt.m_paramObjects[icol])[irow];
/* 138 */     Reader reader = readerParam.m_reader;
/* 139 */     long targetLen = readerParam.m_targetLen;
/*     */     
/* 141 */     String serverEncoding = this.pstmt.connection.getServerEncoding();
/*     */     
/* 143 */     int retLen = 0;
/* 144 */     String str = null;
/*     */     
/* 146 */     if (this.toBindLobCyc)
/*     */     {
/* 148 */       return -1;
/*     */     }
/*     */     
/* 151 */     if (this.ret != null)
/*     */     {
/* 153 */       int realLen = msg.appendBytesWithLen2(this.ret, 0, this.ret.length);
/* 154 */       this.ret = null;
/* 155 */       return realLen;
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 160 */       int maxLen = 8000;
/* 161 */       this.readerTry = new char[maxLen];
/* 162 */       this.readerTryLen = reader.read(this.readerTry, 0, maxLen);
/* 163 */       if (this.readerTryLen < maxLen)
/*     */       {
/* 165 */         if (this.readerTryLen == -1) {
/* 166 */           this.readerTryLen = 0;
/*     */         }
/* 168 */         this.streamReadOver = true;
/*     */       }
/*     */       
/* 171 */       int charsLen = this.readerTryLen;
/* 172 */       if ((targetLen != -1L) && (charsLen > targetLen))
/*     */       {
/* 174 */         charsLen = (int)targetLen;
/* 175 */         this.streamReadOver = true;
/*     */       }
/*     */       
/* 178 */       str = new String(this.readerTry, 0, charsLen);
/*     */       
/* 180 */       if ((this.destDType == 12) || (this.destDType == 19))
/*     */       {
/* 182 */         this.ret = Convertion.getBytes(str, serverEncoding);
/* 183 */         retLen = this.ret.length;
/*     */         
/* 185 */         if (retLen > 2048)
/*     */         {
/* 187 */           this.toBindLobCyc = true;
/* 188 */           return -1;
/*     */         }
/*     */       }
/*     */       
/* 192 */       switch (this.destDType)
/*     */       {
/*     */       case 0: 
/*     */       case 1: 
/* 196 */         if (!this.streamReadOver)
/* 197 */           str = readLeftCompletelyWithTry(readerParam, str);
/* 198 */         this.ret = Convertion.String_char_2CType(str, this.destPrec, this.destDType, serverEncoding);
/*     */         
/* 200 */         break;
/*     */       case 2: 
/*     */       case 19: 
/* 203 */         if (!this.streamReadOver)
/* 204 */           str = readLeftCompletelyWithTry(readerParam, str);
/* 205 */         this.ret = Convertion.String_varchar_2CType(str, this.destPrec, this.destDType, serverEncoding);
/* 206 */         break;
/*     */       
/*     */       case 12: 
/*     */       case 17: 
/*     */       case 18: 
/* 211 */         if (!this.streamReadOver)
/*     */         {
/* 213 */           str = readLeftCompletelyWithTry(readerParam, str);
/*     */         }
/* 215 */         this.ret = Convertion.getBytes(str, serverEncoding);
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 220 */         break;
/*     */       default: 
/* 222 */         DBError.throwSQLException(6007);
/*     */       }
/*     */       
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 228 */       throw new SQLException(e.getMessage());
/*     */     }
/*     */     
/* 231 */     clearVars();
/* 232 */     return msg.appendBytesWithLen2(this.ret, 0, this.ret.length);
/*     */   }
/*     */   
/*     */   public int bindLobCyc(DmMsgSend lobMsg, int irow, int icol) throws SQLException
/*     */   {
/* 237 */     DmReaderParam readerParam = ((DmReaderParam[])this.pstmt.m_paramObjects[icol])[irow];
/* 238 */     Reader reader = readerParam.m_reader;
/* 239 */     long targetLen = readerParam.m_targetLen;
/*     */     
/* 241 */     int realLen = 0;
/* 242 */     int retLen = 0;
/* 243 */     String serverEncoding = this.pstmt.connection.getServerEncoding();
/*     */     
/* 245 */     this.toBindLobCyc = false;
/*     */     
/* 247 */     if (this.readerTry != null)
/*     */     {
/* 249 */       realLen = this.readerTryLen;
/* 250 */       if ((targetLen != -1L) && (this.readerTryLen > targetLen)) {
/* 251 */         realLen = (int)targetLen;
/*     */       }
/* 253 */       String str = new String(this.readerTry, 0, realLen);
/* 254 */       byte[] ret = Convertion.getBytes(str, serverEncoding);
/*     */       
/* 256 */       retLen = lobMsg.appendOffRowBytes(ret, 0, ret.length, this.new_lob_flag, str.length());
/* 257 */       this.readerTry = null;
/* 258 */       return retLen;
/*     */     }
/*     */     
/* 261 */     if (this.streamReadOver)
/*     */     {
/* 263 */       clearVars();
/* 264 */       return 0;
/*     */     }
/*     */     
/* 267 */     int maxLen = 8000;
/* 268 */     char[] charsBuf = new char[maxLen];
/*     */     
/*     */     try
/*     */     {
/* 272 */       retLen = reader.read(charsBuf, 0, maxLen);
/* 273 */       if (retLen == -1)
/*     */       {
/* 275 */         clearVars();
/* 276 */         return 0;
/*     */       }
/*     */       
/*     */ 
/* 280 */       if (retLen < maxLen) {
/* 281 */         this.streamReadOver = true;
/*     */       }
/* 283 */       this.readerTryLen += retLen;
/* 284 */       if ((targetLen != -1L) && (this.readerTryLen >= targetLen))
/*     */       {
/* 286 */         this.streamReadOver = true;
/* 287 */         retLen = (int)(retLen - (this.readerTryLen - targetLen));
/*     */       }
/*     */       
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 293 */       throw new SQLException(e.getMessage());
/*     */     }
/*     */     
/* 296 */     String subStr = new String(charsBuf, 0, retLen);
/*     */     byte[] strbs;
/* 298 */     byte[] strbs; if ((this.destDType == 12) || (this.destDType == 17) || (this.destDType == 18))
/*     */     {
/* 300 */       strbs = Convertion.stringToBinary(subStr, this.destPrec);
/*     */     }
/*     */     else {
/* 303 */       strbs = Convertion.getBytes(subStr, serverEncoding);
/*     */     }
/*     */     
/* 306 */     return lobMsg.appendOffRowBytes(strbs, 0, strbs.length, this.new_lob_flag, subStr.length());
/*     */   }
/*     */   
/*     */   public void clearRetValue()
/*     */   {
/* 311 */     this.ret = null;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\driver\DmReaderBinder.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */